package com.dd.stack.mybatisplus.service.inner.impl.multi_table;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dd.stack.mybatisplus.mapper.multi_table.OrderMapper;
import com.dd.stack.mybatisplus.pojo.BO.OrderDetailDTO;
import com.dd.stack.mybatisplus.pojo.BO.OrderStatsDTO;
import com.dd.stack.mybatisplus.pojo.BO.OrderWithUserDTO;
import com.dd.stack.mybatisplus.pojo.req.OrderQueryReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author liuxianmeng
 * @CreateTime 2025/2/25 0:38
 * @Description 类功能作用说明
 */
@Service
public class OrderService {
    @Autowired
    private OrderMapper orderMapper;

    // 查询订单详情及用户信息
    public OrderDetailDTO getOrderDetailsWithUserInfo(Long orderId) {
        return orderMapper.getOrderDetailsWithUserInfo(orderId);
    }

    // 分页查询订单及其用户信息
    public List<OrderWithUserDTO> getOrdersWithUsers(OrderQueryReq queryDTO) {
        Map<String, Object> params = new HashMap<>();
        params.put("orderId", queryDTO.getOrderId());
        params.put("orderNumber", queryDTO.getOrderNumber());
        params.put("status", queryDTO.getStatus());
        params.put("username", queryDTO.getUsername());
        params.put("startDate", queryDTO.getStartDate());
        params.put("endDate", queryDTO.getEndDate());
        params.put("offset", (queryDTO.getCurrent() - 1) * queryDTO.getSize());
        params.put("limit", queryDTO.getSize());
        return orderMapper.getOrdersWithUsers(params);
    }

    // 查询每个用户的订单统计信息
    public List<OrderStatsDTO> getOrderStatsByUser() {
        return orderMapper.getOrderStatsByUser();
    }

    public IPage<OrderWithUserDTO> getOrdersWithUsersByPage(OrderQueryReq queryDTO) {
        // 构造分页参数
        Page<OrderWithUserDTO> page = new Page<>(queryDTO.getCurrent(), queryDTO.getSize());

        // 构造查询条件
        Map<String, Object> params = new HashMap<>();
        params.put("status", queryDTO.getStatus());
        params.put("username", queryDTO.getUsername());
        params.put("startDate", queryDTO.getStartDate());
        params.put("endDate", queryDTO.getEndDate());

        // 调用 Mapper 层的分页查询方法
        return orderMapper.getOrdersWithUsersByPage(page, params);
    }
}
